<?php

/**
 * sfWidgetFormDoctrineCountryStatus.
 *
 * @package    scart
 * @subpackage actions
 * @author     Pedro Nilton Olivera Chamochumbi <poliverach@gmail.com>
 * @author     Jonathan Olger Nieto Lajo <jonathan_nieto@hotmail.com>
 */

class sfWidgetFormDoctrineCountrySelect extends sfWidgetFormDoctrineSelect
{
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure();
    
    $this->setOption('model', 'Country');
  }
  
  public function getChoices()
  {
    $choices = array();
    $method  = $this->getOption('method');
    
    if (false !== $this->getOption('add_empty'))
    {
      $choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
    }

    $a = $this->getOption('alias');
    $q = is_null($this->getOption('query'))
        ? Doctrine::getTable($this->getOption('model'))->createQuery($a)
        : $this->getOption('query');

    $q1 = clone $q;
    $q1->where($a.'.status = 1')
       ->andWhere($a.'.by_default = 1')
       ->orderBy($a.'.orden ASC');
    
    $objects = $q1->execute();
    foreach ($objects as $object)
    {
      $choices['----'][is_array($value = $object->getPrimaryKey()) ? current($value) : $value] = $object->$method();
    }

    $q2 = clone $q;
    $q2->where($a.'.status = 1')
       ->orderBy($a.'.orden ASC');
    
    $objects = $q2->execute();
    foreach ($objects as $object)
    {
      $choices['--------'][is_array($value = $object->getPrimaryKey()) ? current($value) : $value] = $object->$method();
    }

    return $choices;
  }
}